package com.unittec.zk.provider.flowable.service.impl;

import java.util.List;

import com.unittec.zk.provider.flowable.common.constant.ServiceIdConstants;
import com.unittec.zk.provider.flowable.service.ISysDeployFormService;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.scope.ThreadLocalScope;
import com.unittec.zk.provider.service.HystrixRestTemplateService;
import com.unittec.zk.sdk.auth.domain.SysDeployFormDto;
import com.unittec.zk.sdk.auth.domain.SysFormDto;
import com.unittec.zk.sdk.auth.request.*;
import com.unittec.zk.sdk.auth.response.*;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.BizCode;
import com.unittec.zk.sdk.root.internal.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 流程实例关联表单Service业务层处理
 *
 * @author XuanXuan Xuan
 * @date 2021-04-03
 */
@Service
public class SysDeployFormServiceImpl implements ISysDeployFormService
{
    @Autowired
    Log log;

    @Resource
    private HystrixRestTemplateService hystrixRestTemplateService;

    /**
     * 查询流程实例关联表单
     *
     * @param id 流程实例关联表单ID
     * @return 流程实例关联表单
     */
    @Override
    public SysDeployFormDto selectSysDeployFormById(Long id)
    {
        log.sysInfoLog("selectSysDeployFormById ", "begin");
        try {
            GatewayRequest<Req101661> getWay = new GatewayRequest<>();
            Req101661 req = new Req101661();
            req.setId(id);
            getWay.setContent(req);
            getWay.setMethod("101661");
            Result<Res101661> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101661, getWay,
                    ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101661>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "流程实例关联表单失败");
                throw new BusinessException("流程实例关联表单失败");
            }else{
                if (result.getData() != null && result.getData().getDeployForm() != null) {
                   SysDeployFormDto sysDeployForm = result.getData().getDeployForm();
                   return  sysDeployForm;
                }
            }
        } catch (BeansException e) {
            log.sysErrLog("selectSysDeployFormById", "流程实例关联表单失败", e);
            throw new BusinessException("流程实例关联表单失败");
        }
        log.sysInfoLog("selectSysDeployFormById", "end");
        return  null;
    }

    /**
     * 查询流程实例关联表单列表
     *
     * @param sysDeployForm 流程实例关联表单
     * @return 流程实例关联表单
     */
    @Override
    public List<SysDeployFormDto> selectSysDeployFormList(SysDeployFormDto sysDeployForm)
    {
        log.sysInfoLog("selectSysDeployFormList ", "begin");
        try {
            GatewayRequest<Req101663> getWay = new GatewayRequest<>();
            Req101663 req = new Req101663();
            req.setSysDeployForm(sysDeployForm);
            getWay.setContent(req);
            getWay.setMethod("101663");
            Result<Res101663> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101663, getWay,
                    ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101663>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "流程实例关联表单列表失败");
                throw new BusinessException("流程实例关联表单列表失败");
            }else{
                if (result.getData() != null && result.getData().getList() != null) {
                    List<SysDeployFormDto> list = result.getData().getList();
                    return  list;
                }
            }
        } catch (BeansException e) {
            log.sysErrLog("selectSysDeployFormList", "流程实例关联表单列表失败", e);
            throw new BusinessException("流程实例关联表单列表失败");
        }
        log.sysInfoLog("selectSysDeployFormList", "end");
        return  null;
    }

    /**
     * 新增流程实例关联表单
     *
     * @param sysDeployForm 流程实例关联表单
     * @return 结果
     */
    @Override
    public int insertSysDeployForm(SysDeployFormDto sysDeployForm)
    {
        log.sysInfoLog("insertSysDeployForm ", "begin");
        try {
            GatewayRequest<Req101664> getWay = new GatewayRequest<>();
            Req101664 req = new Req101664();
            req.setSysDeployForm(sysDeployForm);
            getWay.setContent(req);
            getWay.setMethod("101664");
            Result<Res101664> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101664, getWay,
                    ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101664>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "新增流程实例关联表单失败");
                throw new BusinessException("新增流程实例关联表单失败");
            }else{
                if (result.getData() != null) {// && result.getData().geContent() != null
                    String content = result.getData().getContent();//.geContent()
                    if(content == "导入表单成功"){
                        return 0;
                    }else{
                        return  1;
                    }
                }
            }
        } catch (BeansException e) {
            log.sysErrLog("insertSysDeployForm", "新增流程实例关联表单失败", e);
            throw new BusinessException("新增流程实例关联表单失败");
        }
        log.sysInfoLog("insertSysDeployForm", "end");
        return  1;
    }

    /**
     * 修改流程实例关联表单
     *
     * @param sysDeployForm 流程实例关联表单
     * @return 结果
     */
    @Override
    public int updateSysDeployForm(SysDeployFormDto sysDeployForm)
    {
        //return sysDeployFormMapper.updateSysDeployForm(sysDeployForm);
        log.sysInfoLog("updateSysDeployForm ", "begin");
        try {
            GatewayRequest<Req101665> getWay = new GatewayRequest<>();
            Req101665 req = new Req101665();
            req.setSysDeployForm(sysDeployForm);
            getWay.setContent(req);
            getWay.setMethod("101665");
            Result<Res101665> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101665, getWay,
                    ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101665>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "修改流程实例关联表单失败");
                throw new BusinessException("修改流程实例关联表单失败");
            }else{
                if (result.getData() != null ) {//&& result.getData().geContent() != null
                    String content = result.getData().toString();//geContent()
                    if(content == "导入表单成功"){
                        return 0;
                    }else{
                        return  1;
                    }
                }
            }
        } catch (BeansException e) {
            log.sysErrLog("updateSysDeployForm", "修改流程实例关联表单失败", e);
            throw new BusinessException("修改流程实例关联表单失败");
        }
        log.sysInfoLog("updateSysDeployForm", "end");
        return  1;
    }

    /**
     * 批量删除流程实例关联表单
     *
     * @param ids 需要删除的流程实例关联表单ID
     * @return 结果
     */
    @Override
    public int deleteSysDeployFormByIds(Long[] ids)
    {
        //return sysDeployFormMapper.deleteSysDeployFormByIds(ids);
        log.sysInfoLog("deleteSysDeployFormByIds ", "begin");
        try {
            GatewayRequest<Req101666> getWay = new GatewayRequest<>();
            Req101666 req = new Req101666();
            req.setIds(ids);
            getWay.setContent(req);
            getWay.setMethod("101666");
            Result<Res101666> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101666, getWay,
                    ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101666>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "批量删除流程实例关联表单失败");
                throw new BusinessException("批量删除流程实例关联表单失败");
            }else{
                if (result.getData() != null ) {//&& result.getData().geContent() != null
                    String content = result.getData().toString();//geContent()
                    if(content == "批量删除表单成功"){
                        return 0;
                    }else{
                        return  1;
                    }
                }
            }
        } catch (BeansException e) {
            log.sysErrLog("deleteSysDeployFormByIds", "批量删除流程实例关联表单失败", e);
            throw new BusinessException("批量删除流程实例关联表单失败");
        }
        log.sysInfoLog("deleteSysDeployFormByIds", "end");
        return  1;
    }

    /**
     * 删除流程实例关联表单信息
     *
     * @param id 流程实例关联表单ID
     * @return 结果
     */
    @Override
    public int deleteSysDeployFormById(Long id)
    {
        //return sysDeployFormMapper.deleteSysDeployFormById(id);
        log.sysInfoLog("deleteSysDeployFormById ", "begin");
        try {
            GatewayRequest<Req101667> getWay = new GatewayRequest<>();
            Req101667 req = new Req101667();
            req.setId(id);
            getWay.setContent(req);
            getWay.setMethod("101667");
            Result<Res101667> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101667, getWay,
                    ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101667>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "删除流程实例关联表单失败");
                throw new BusinessException("删除流程实例关联表单失败");
            }else{
                if (result.getData() != null ) {//&& result.getData().geContent() != null
                    String content = result.getData().toString();//geContent()
                    if(content == "删除表单成功"){
                        return 0;
                    }else{
                        return  1;
                    }
                }
            }
        } catch (BeansException e) {
            log.sysErrLog("deleteSysDeployFormById", "删除流程实例关联表单失败", e);
            throw new BusinessException("删除流程实例关联表单失败");
        }
        log.sysInfoLog("deleteSysDeployFormById", "end");
        return  1;
    }

    /**
     * 查询流程挂着的表单
     *
     * @param deployId
     * @return
     */
    @Override
    public SysFormDto selectSysDeployFormByDeployId(String deployId) {
        //return sysDeployFormMapper.selectSysDeployFormByDeployId(deployId);
        log.sysInfoLog("selectSysDeployFormByDeployId ", "begin");
        try {
            GatewayRequest<Req101662> getWay = new GatewayRequest<>();
            Req101662 req = new Req101662();
            req.setDeployId(deployId);
            getWay.setContent(req);
            getWay.setMethod("101662");
            Result<Res101662> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101662, getWay,
                    ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101662>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "查询流程挂着的表单失败");
                throw new BusinessException("查询流程挂着的表单失败");
            }else{
                if (result.getData() != null && result.getData().getObj() != null) {
                    SysFormDto sysForm = result.getData().getObj();
                    return  sysForm;
                }
            }
        } catch (BeansException e) {
            log.sysErrLog("selectSysDeployFormById", "查询流程挂着的表单失败", e);
            throw new BusinessException("查询流程挂着的表单失败");
        }
        log.sysInfoLog("selectSysDeployFormByDeployId", "end");
        return  null;
    }
}
